package main

import (
	"fmt"
	"sync"
	"time"
)

func send5(ch chan int, wg *sync.WaitGroup) {
	defer wg.Done()
	i := 0
	for i < 4 {
		i++
		ch <- i
	}
}

func recv5(ch chan int) {
	for v := range ch {
		fmt.Println(v)
	}
}

func main() {
	var ch = make(chan int, 4)
	var wg = new(sync.WaitGroup)
	wg.Add(2) // 增加计数值
	go recv5(ch)
	go send5(ch, wg)
	go send5(ch, wg)

	// wait 阻塞等待所有的写通道协程结束
	wg.Wait()
	close(ch)
	time.Sleep(time.Second)
}
